package com.qeegoo.b2bautozityp.moudle.goods.view;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.adapter.GoodsAdapter;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.api.entity.HttpResult;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.databinding.ActivitySearchGoodsBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerGoodsManagerComponent;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.base.BaseAdapter;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsManagerBean;
import com.qeegoo.b2bautozityp.moudle.goods.viewmodel.GoodItemViewModel;
import com.qeegoo.b2bautozityp.moudle.goods.viewmodel.GoodsSearchViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.lib.util.NavigateUtils;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by lk on 2017/9/28.
 */

public class GoodsSearchActivity extends BaseActivity<ActivitySearchGoodsBinding> {
    private Subscription subscription;

    @Inject
    GoodsSearchViewModel viewModel;

    @Inject
    LinearLayoutManager mLayuoutManager;
    private GoodsManagerBean resultData;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_search_goods;
    }

    @Override
    protected void initInjector() {
        DaggerGoodsManagerComponent.builder().appComponent(getAppComponent()).activityComponent(getActivityComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        mBinding.setViewModel(viewModel);
        mBinding.orderRv.setLayoutManager(mLayuoutManager);
        BaseAdapter adapter = new GoodsAdapter(BR.goodsAdapter, R.layout.adapter_goods_manager, new ArrayList<GoodItemViewModel>());
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            GoodItemViewModel viewModel = (GoodItemViewModel) adapter.getItem(position);
            String goodId = viewModel.detailBean.id;
            Bundle bundle = new Bundle();
            bundle.putString(Constants.Param_id, goodId);
            NavigateUtils.startActivity(GoodsDetailActivity.class, bundle);
        });
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mBinding.orderRv.setAdapter(adapter);
        viewModel.setAdapterInterface(data -> {


            resultData = data;
            List<GoodItemViewModel> datas = new ArrayList<GoodItemViewModel>();
            ;

            if (data.list == null || data.list.size() == 0) {
                viewModel.emptyVisible.set(View.VISIBLE);
            } else {
                int position = 0;
                for (GoodsManagerBean.GoodDetailBean detailBean : data.list) {
                    detailBean.position = position++;
                    datas.add(new GoodItemViewModel(detailBean,viewModel.requestApi,adapter));
                }
                viewModel.emptyVisible.set(View.GONE);
            }
            if (data.curPageNo == 1) {
                adapter.replaceData(datas);
            } else {
                adapter.addData(datas);
            }
        });
        subscription = RxTextView.textChanges(mBinding.tvTitle)
                .subscribeOn(AndroidSchedulers.mainThread())//
                .debounce(400, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .filter(new Func1<CharSequence, Boolean>() {
                    @Override
                    public Boolean call(CharSequence charSequence) {
                        return charSequence.toString().trim().length() > 0;
                    }
                })
                .observeOn( Schedulers.newThread() )
                .switchMap(new Func1<CharSequence, Observable<HttpResult<GoodsManagerBean>>>() {
                    @Override
                    public Observable<HttpResult<GoodsManagerBean>> call(CharSequence charSequence) {
                        return viewModel.getData(charSequence.toString());
                    }
                })
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread(), true)
                .subscribe(goodsManagerBean -> {
                    resultData = goodsManagerBean;
                    List<GoodItemViewModel> datas = new ArrayList<GoodItemViewModel>();
                    ;

                    if (goodsManagerBean.list == null || goodsManagerBean.list.size() == 0) {
                        viewModel.emptyVisible.set(View.VISIBLE);
                    } else {

                        for (GoodsManagerBean.GoodDetailBean detailBean : goodsManagerBean.list) {
                            detailBean.pageType = 1;
                            datas.add(new GoodItemViewModel(detailBean,viewModel.requestApi,adapter));
                        }
                        viewModel.emptyVisible.set(View.GONE);
                    }
                    adapter.replaceData(datas);
                }, er -> {

                    showLoading();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }
}
